﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using HROReporting.Infrastructure.CookieStorage;
using System.Globalization;
using HROReporting.Controllers.ViewModels.PresentieLijst;
using HROReporting.Infrastructure.Authentication;
using HROReporting.Services.Interfaces;
using HROReporting.Services.Messaging.NFCDeviceService;
using HROReporting.Services.Messaging.DocentService;
using HROReporting.Model.Lesuren;

namespace HROReporting.Controllers.Controllers
{
    [Authorize(Roles = "Docent")]
    public class PresentieLijstController : BaseController
    {
        private readonly IFormsAuthentication _formsAuthentication;
        private readonly IDocentService _docentService;
        private readonly INFCDeviceService _nfcDeviceService;
        private readonly ILesuurService _lesuurService;

        public PresentieLijstController(ICookieStorageService cookieStorageService,
                                        IFormsAuthentication formsAuthentication,
                                        IDocentService docentService,
                                        INFCDeviceService nfcDeviceService,
                                        ILesuurService lesuurService)
            : base (cookieStorageService)
        {
            _formsAuthentication = formsAuthentication;
            _docentService = docentService;
            _nfcDeviceService = nfcDeviceService;
            _lesuurService = lesuurService;
        }
        
        public ActionResult Index()
        {
            return View();
        }

        
        public ActionResult Create()
        {
            PresentielijstCreateView presentielijstView = new PresentielijstCreateView();

            GetAllNFCDevicesResponse response = _nfcDeviceService.GetAllNFCDevices();

            presentielijstView.NFCDeviceSerials = response.NFCDevices;

            IEnumerable<Lesuur> lesuren = _lesuurService.FindAll();

            presentielijstView.LesStartUren = lesuren;
            presentielijstView.LesEindUren = lesuren;

            return View(presentielijstView);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(PresentielijstCreateView presentielijstView)
        {
            GetDocentByRequest docentRequest = new GetDocentByRequest();
            docentRequest.AuthenticationToken = _formsAuthentication.GetAuthenticationToken();
            GetDocentByResponse docentResponse = _docentService.GetDocentByAuthenticationToken(docentRequest);

            GetNFCDeviceByRequest nfcRequest = new GetNFCDeviceByRequest();
            nfcRequest.NFCDeviceSerial = presentielijstView.SelectedNFCDevice;
            GetNFCDeviceByResponse nfcResponse = _nfcDeviceService.GetNFCDeviceByDeviceSerial(nfcRequest);

            Lesuur lesuurStart = _lesuurService.FindBy(Convert.ToInt16(presentielijstView.SelectedLesuurStart));
            Lesuur lesuurEind = _lesuurService.FindBy(Convert.ToInt16(presentielijstView.SelectedLesuurEind));
            
            CreatePresentielijstRequest createPresentieRequest = new CreatePresentielijstRequest();
            createPresentieRequest.Docent = docentResponse.DocentModel;
            createPresentieRequest.NFCDevice = nfcResponse.NFCDevice;
            createPresentieRequest.LesuurStart = lesuurStart;
            createPresentieRequest.LesuurEind = lesuurEind;
            createPresentieRequest.Datum = DateTime.Now;
            createPresentieRequest.Opmerkingen = presentielijstView.Opmerkingen;

            _docentService.CreatePresentielijst(createPresentieRequest);

            return Redirect("/Docent/MyAccount");
        }

        public ActionResult Details(int id)
        {
            PresentielijstDetailView detailView = new PresentielijstDetailView();
            
            detailView.Presentielijst = _docentService.GetPresentielijstById(id);
            detailView.PresentieInchecking = _docentService.GetPresentieByLijstId(id);

            return View(detailView);
        }
    }
}
